import router from '@/router'
const loadView = (name) => {
  return () => import('_views/' + name)
}
export default {
  namespaced: true,
  state: {
    // 侧边导航路由菜单
    asideNavs:
      localStorage.getItem('navs')
        ? JSON.parse(localStorage.getItem('navs'))
        : [],
    // 当前角色可以访问 权限路由数组
    authRoutes: []
  },
  mutations: {
    INIT_NAV (state, navs) {
      // 动态添加侧边导航
      state.asideNavs = navs
    },
    INIT_AUTH_ROUTES (state, authRoutes) {
      // 动态添加路由
      /*
        动态添加路由
        router.addRoute('admin', 动态路由)
        {
          path: '/dashBoard',
          name: 'dashBoard',
          component: 'DashBoard'
        }
        component存储的是 字符串 路由组件名字
        变成
        {
          path: '/dashBoard',
          name: 'dashBoard',
          component: () => import('DashBoard')
        }
      */
      authRoutes.forEach(route => {
        route.component = loadView(route.component)
        // 动态添加到路由上 作为admin二级路由
        router.addRoute('admin', route)
      })
      // 动态添加路由 404不能作为基础路由定义 必须作为动态路由添加
      router.addRoute({
        path: '*',
        component: () => import('_views/NotFound')
      })
    }
  }
}
